<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <script type="text/javascript" src="/js/jquery-3.5.1.js"></script>
    <title>jquery ajax post</title>
</head>


jquery ajax test <br>

<input type='button' value='get data' onclick='getData()' ondblclick="getData()">

<hr>
<div id='data1'> </div>

<hr>
<div id='data2'> </div>

<hr>
<div id='data3'> </div>


<script>

    function getData() {
        // alert('hello2');
        var user = { "id": 9, "name": "tiger" };
        alert('hello,' + JSON.stringify(user));

        // ajax get
        $.ajax({
            url: "/data/getUser",
            data: user,
            success: function (data) {
                console.log(data);
                $('#data1').html(JSON.stringify(data));
            }
        });

        // ajax post
        var user2 = { "id": 9, "name": "tiger2" };
        $.ajax({
            type: "post",
            url: "/data/getUser",
            data: user2,
            success: function (data) {
                console.log(data);
                $('#data2').html(JSON.stringify(data));
            }
        });
        // ajax post json 
        var user3 = { "id": 9, "name": "tiger3" };

        $.ajax({
            // 默认 get
            type: "post",
            // contentType 默认 application/x-www-form-urlencoded
            contentType: "application/json; charset=utf-8",
            //  预期的服务器响应的数据类型 ,可以不写
            // dataType: "json", 
            url: "/data/getUserFromJson",
            // 一定要转成string
            data: JSON.stringify(user3),
            // data:user3,
            success: function (data) {
                console.log(data);
                $('#data3').html(JSON.stringify(data));
            }
        });
    }

</script>